#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define filename "log.txt"

/*

int main()
{
    //test1()
    //close(0  close(1)  close(2)
    
    
    //fd(文件名,打开模式,权限设置)
    int fd=open(filename,O_WRONLY|O_CREAT|O_APPEND,0666);
    //int fd=open(filename,O_RDONLY,0666);
    //检查
    if(fd<0)
    {
        perror("open fail!\n");
        return 1;
    }

    //重定向
    dup2(fd,1);
    close(fd);
    //下面三个都是往显示器文件写入
    printf("fd:%d\n",fd);
    printf("hello printf\n");
    fprintf(stdout,"hello fprintf\n");





    
     


    //输入重定向
    dup2(fd,0);
     
    char inbuffer[1024];
    //这里-1是留给\0的
    ssize_t s=read(0,inbuffer,sizeof(inbuffer)-1);
    if(s>0)
    {
        inbuffer[s]='\0';
        printf("echo# %s\n",inbuffer);
    }


    


    
    //重定向
    dup2(fd,1);


    // printf("fd:%d\n",fd);
    int cnt=5;
    const char* message="hello linux!\n";
    while(cnt--)
    {
        //写入write(文件描述符,信息,信息长度)
        write(1,message,strlen(message));
    }
    
    close(fd);

    


    return 0;
}*/


int main()
{
    fprintf(stdout,"hello linux!\n");
    fprintf(stdout,"hello linux!\n");
    fprintf(stdout,"hello linux!\n");
    fprintf(stdout,"hello linux!\n");
    fprintf(stdout,"hello linux!\n");

    fprintf(stderr,"hello linux!-err\n");
    fprintf(stderr,"hello linux!-err\n");
    return 0;
}
